﻿using System;
public class Node<T>
{
    public Node<T> Next { get; set; }
    public T Data { get; set; }

    public Node(T t)
    {
        Next = null;
        Data = t;
    }
}

public class GenericList<T>
{
    private Node<T> head;
    private Node<T> tail;

    public GenericList()
    {
        tail = head = null;
    }

    public Node<T> Head
    {
        get => head;
    }

    public void Add(T t)
    {
        Node<T> n = new Node<T>(t);
        if (tail == null)
        {
            head = tail = n;
        }
        else
        {
            tail.Next = n;
            tail = n;
        }
    }

    public void ForEach(Action<T> action)
    {
        Node<T> current = head;
        while (current != null)
        {
            action(current.Data);
            current = current.Next;
        }
    }
}

class Program
{
    static void Main(string[] args)
    {
        
        GenericList<int> intList = new GenericList<int>();
        for (int x = 0; x < 10; x++)
        {
            intList.Add(x);
        }

        Console.WriteLine("打印链表：");
        intList.ForEach(x => Console.Write(x+" "));
        Console.WriteLine();
        int sum = 0;
        intList.ForEach(x => sum += x);
        Console.WriteLine($"总和：{sum}");

        //判断非空
        if (intList.Head != null)
        {
            int max = intList.Head.Data;
            int min = intList.Head.Data;
            intList.ForEach(x => {
                max = x > max ? x : max;
                min = x < min ? x : min;
            });
            Console.WriteLine($"最大值：{max}");
            Console.WriteLine($"最小值：{min}");
        }

       
    }
}
